# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=protobuf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# Note: make sure to update python-protobuf to match this version
pkgver=30.2
pkgrel=4
pkgdesc="Protocol Buffers - Google's data interchange format (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://developers.google.com/protocol-buffers/'
msys2_repository_url="https://github.com/protocolbuffers/protobuf"
msys2_references=(
  'archlinux: protobuf'
  "cpe: cpe:/a:google:google-protobuf"
  "cpe: cpe:/a:google:protobuf"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/protocolbuffers/${_realname}/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-fix-building-shared-libs-with-clang.patch
        0002-windres-invocation.patch
        0003-demote-error-about-buildtime-runtime-version-difference-to-warning.patch
        0004-fix-build-with-gcc-15.patch)
sha256sums=('fb06709acc393cc36f87c251bb28a5500a2e12936d4346099f2c6240f6c7a941'
            '04096f913c1fd5b8c6703e3f696ae7d068c1576ef14fadad2e372da4af188dd0'
            '174f714b842d5153c79c5fda1ae775ee002aea11d53cb5d5f51cb5c4b9e63d29'
            '2b680e1c642ccd5bb70b3dcf7217c27360bc0b74f63d8afb66dd668173d9b2dd'
            'cb3594f1412422681768709c63139622b9f89c21f12b9bafa256ca44df8daa51')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-fix-building-shared-libs-with-clang.patch \
    0002-windres-invocation.patch \
    0003-demote-error-about-buildtime-runtime-version-difference-to-warning.patch

  # https://github.com/protocolbuffers/protobuf/issues/21333
  if [[ ${CC} == gcc ]]; then
    apply_patch_with_msg \
      0004-fix-build-with-gcc-15.patch
  fi
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  echo "Building static library"
  mkdir -p  "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      -Dprotobuf_BUILD_SHARED_LIBS="OFF" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  echo "Building shared library"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  CXXFLAGS+=" -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      -Dprotobuf_BUILD_SHARED_LIBS="ON" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
